﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vara_Data_Layer.Vara_Data_Asset;

namespace Vara_Data_Layer.Vara_Data_Audit_History
{
    public class Vara_Data_Audit_History_Controller : Vara_Data_IAudit_History_Actions
    {
        private List<Vara_Data_IAudit_History> _AuditHistories;
        public List<Vara_Data_IAudit_History> AuditHistories
        {
            get
            {
                if (_AuditHistories == null)
                    Refresh_Audit_History_List();
                return _AuditHistories;
            }
        }
        public Vara_Data_IAudit_History ReturnAuditHistory(int ID)
        {
            Vara_Data_IAudit_History AuditHistoryToReturn;
            AuditHistoryToReturn = AuditHistories.Find(delegate(Vara_Data_IAudit_History AuditHistory) { return AuditHistory.ID == ID; });
            return AuditHistoryToReturn;
        }
        public void Refresh_Audit_History_List()
        {
            //DataTable DataTableAuditHistories;
            //using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
            //{
            //    DataTableAuditHistories = NewSqlEngine.ExecuteProcedure("Return_Audit_Histories");
            //}
            //List<Vara_Data_IAudit_History> ListOfAuditHistories = new List<Vara_Data_IAudit_History>();
            //foreach (DataRow R in DataTableAuditHistories.Rows)
            //{
            //    Vara_Data_IAudit_History AuditHistoryToAppend = new Vara_Data_Audit_History(int.Parse(R["ID"].ToString()));
            //    AuditHistoryToAppend.AssetID= int.Parse(R["AssetID"].ToString());
            //    AuditHistoryToAppend.Date = DateTime.Parse(R["Date"].ToString());
            //    AuditHistoryToAppend.CompletedBy = R["CompletedBy"].ToString();
            //    AuditHistoryToAppend.OutcomeID = int.Parse(R["Audit_OutcomeID"].ToString());
            //    AuditHistoryToAppend.Deleted = bool.Parse(R["Deleted"].ToString());
            //    AuditHistoryToAppend.Actions = R["Actions"].ToString();
            //    ListOfAuditHistories.Add(AuditHistoryToAppend);
            //}
            //_AuditHistories = ListOfAuditHistories;
        }
        public List<Vara_Data_IAudit_History> AuditHistoriesForAsset(int AssetID)
        {
            List<Vara_Data_IAudit_History> AuditHistoriesToReturn;
            AuditHistoriesToReturn = AuditHistories.FindAll(delegate(Vara_Data_IAudit_History AuditHistory) { return AuditHistory.AssetID == AssetID; });
            return AuditHistoriesToReturn;
        }
        public DataTable AllAuditsForAsset(int AssetID)
        {
            DataTable DataTableAuditHistories;
            using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
            { 
                DataTableAuditHistories = NewSqlEngine.ExecuteProcedure("Return_Asset_Audit_Histories",new SqlEngineProcedureVariable("@AssetID",AssetID,SqlDbType.Int));
            }
            return DataTableAuditHistories;
        }
        public void DeleteAuditHistory(int AuditHistoryID)
        {
            using (SqlEngine NewSqlEngine = new SqlEngine("AssetTracking"))
            {
                NewSqlEngine.ExecuteProcedure("Delete_Audit_History", new SqlEngineProcedureVariable("@AuditHistoryID", AuditHistoryID, SqlDbType.Int));
            }
        }
    }
}
